package com.huilian.cloud.module.system.controller.admin.usergroup;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.huilian.cloud.framework.common.pojo.PageParam;
import com.huilian.cloud.framework.common.pojo.PageResult;
import com.huilian.cloud.framework.common.pojo.CommonResult;
import com.huilian.cloud.framework.common.util.object.BeanUtils;
import static com.huilian.cloud.framework.common.pojo.CommonResult.success;

import com.huilian.cloud.framework.excel.core.util.ExcelUtils;

import com.huilian.cloud.framework.apilog.core.annotation.ApiAccessLog;
import static com.huilian.cloud.framework.apilog.core.enums.OperateTypeEnum.*;

import com.huilian.cloud.module.system.controller.admin.usergroup.vo.*;
import com.huilian.cloud.module.system.dal.dataobject.usergroup.UserGroupDO;
import com.huilian.cloud.module.system.service.usergroup.UserGroupService;

@Tag(name = "管理后台 - 用户与用户组关联")
@RestController
@RequestMapping("/sys/user-group")
@Validated
public class UserGroupController {

    @Resource
    private UserGroupService userGroupService;

    @PostMapping("/create")
    @Operation(summary = "创建用户与用户组关联")
    @PreAuthorize("@ss.hasPermission('sys:user-group:create')")
    public CommonResult<Integer> createUserGroup(@Valid @RequestBody UserGroupSaveReqVO createReqVO) {
        return success(userGroupService.createUserGroup(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户与用户组关联")
    @PreAuthorize("@ss.hasPermission('sys:user-group:update')")
    public CommonResult<Boolean> updateUserGroup(@Valid @RequestBody UserGroupSaveReqVO updateReqVO) {
        userGroupService.updateUserGroup(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户与用户组关联")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('sys:user-group:delete')")
    public CommonResult<Boolean> deleteUserGroup(@RequestParam("id") Integer id) {
        userGroupService.deleteUserGroup(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户与用户组关联")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('sys:user-group:query')")
    public CommonResult<UserGroupRespVO> getUserGroup(@RequestParam("id") Integer id) {
        UserGroupDO userGroup = userGroupService.getUserGroup(id);
        return success(BeanUtils.toBean(userGroup, UserGroupRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户与用户组关联分页")
    @PreAuthorize("@ss.hasPermission('sys:user-group:query')")
    public CommonResult<PageResult<UserGroupRespVO>> getUserGroupPage(@Valid UserGroupPageReqVO pageReqVO) {
        PageResult<UserGroupDO> pageResult = userGroupService.getUserGroupPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UserGroupRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户与用户组关联 Excel")
    @PreAuthorize("@ss.hasPermission('sys:user-group:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserGroupExcel(@Valid UserGroupPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserGroupDO> list = userGroupService.getUserGroupPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户与用户组关联.xls", "数据", UserGroupRespVO.class,
                        BeanUtils.toBean(list, UserGroupRespVO.class));
    }

}